<template>
  <div class="app-container">
    <el-form :model="queryParams" :rules="rules2" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
	  
	  <el-form-item label="学校" prop="sjXxid">
	    <el-select v-model="queryParams.sjXxid" @change="schoolChange" placeholder="请选择学校" :disabled="isXxidFlag">
	      <el-option
	        v-for="d in dept_self"
	        :key="d.value"
	        :label="d.label"
	        :value="d.value"
	      />
	    </el-select>
	  </el-form-item>	
	  <el-form-item label="班级" prop="sjBjid">
	    <el-select v-model="queryParams.sjBjid" @change="classChange" placeholder="请选择班级" >
	  	<el-option label="全部" value=""></el-option>
	  	<el-option
	  	  v-for="c in classList2"
	  	  :key="c.value"
	  	  :label="c.label"
	  	  :value="c.value"
	  	/>
	    </el-select>
		<!-- (<dict-tag :options="tc_nj" :value="queryParams.sjNjid"/>) -->
	  </el-form-item>
<!-- 	  
      <el-form-item label="年级" prop="sjNjid">
        <el-select v-model="queryParams.sjNjid" placeholder="年级" disabled>
          <el-option
            v-for="dict in tc_nj"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
	  -->
      <el-form-item label="体测名称" prop="sjTcnameid">
		  <el-select v-model="queryParams.sjTcnameid" placeholder="体测名称">
			<el-option
			  v-for="c in tcnameList"
			  :key="c.id"
			  :label="c.tcname"
			  :value="c.id"
			/>
		  </el-select>
      </el-form-item>
      <el-form-item label="体测设备" prop="sjSbid">
		<el-select v-model="queryParams.sjSbid" placeholder="体测设备" clearable>
			<el-option label="全部" value=""></el-option>
			<el-option
			  v-for="c in shebeiList"
			  :key="c.sbClientid"
			  :label="c.sbClientid"
			  :value="c.sbClientid"
			/>
		</el-select>
      </el-form-item>
      <el-form-item label="体测项目" prop="sjTcxmid">
			<el-select v-model="queryParams.sjTcxmid" placeholder="体测项目" clearable>
			<el-option label="全部" value=""></el-option>
			<el-option
			  v-for="c in projectList"
			  :key="c.id"
			  :label="c.xmMc"
			  :value="c.id"
			/>
			</el-select>
      </el-form-item>
	  <el-form-item label="证件号" prop="sjSfz">
	    <el-input
	      v-model="queryParams.sjSfz"
	      placeholder="请输入证件号"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item>
<!-- 	  
	  <el-form-item label="姓名" prop="sjSname">
	    <el-input
	      v-model="queryParams.sjSname"
	      placeholder="请输入姓名"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item>
	  -->
<!-- 	  <el-form-item label="学号" prop="sjXh">
	    <el-input
	      v-model="queryParams.sjXh"
	      placeholder="请输入学号"
	      clearable
	      @keyup.enter="handleQuery"
	    />
	  </el-form-item> -->
	  
	  <el-row style="justify-content: right;">
		  <el-form-item>
			<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
			<el-button icon="Refresh" @click="resetQuery">重置</el-button>
		  </el-form-item>
	  </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['tc:tcsj:add']"
        >新增</el-button>
      </el-col>
	  <el-col :span="1.5">
	          <el-button
	            type="success"
	            plain
	            icon="Edit"
	            @click="handleUpdatePL"
	            v-hasPermi="['tc:tcsj:edit']"
	          >数据批量优化</el-button>
          v-hasPermi="['tc:tcsj:export']"
		</el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
        >导出</el-button>
      </el-col> 
      <right-toolbar v-model:showSearch="showSearch" @queryTable="handleQuery"></right-toolbar>
    </el-row>

    <el-table  :show-overflow-tooltip="true" v-loading="loading" :data="tcsjList">
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column type="index" align="center" prop="id" />
      <el-table-column label="学生" align="center" prop="sj_sname" width="150" />
	  <el-table-column label="性别" align="center" prop="sj_sex" >
	    <template #default="scope">
	      <dict-tag :options="sys_user_sex" :value="scope.row.sj_sex"/>
	    </template>
	  </el-table-column>
	  
	  <el-table-column label="得分级别" align="center" prop="sj_dfjb"  width="120" >
	  		<template #default="scope">
	  			<el-tag type="warning">{{ scope.row.sj_dfjb }}</el-tag>
	    </template>
	  </el-table-column>
	  <el-table-column label="体测得分" align="center" prop="sj_fs" >
	  		<template #default="scope">
	  			<el-tag type="danger">{{ scope.row.sj_fs }}</el-tag>
	    </template>
	  </el-table-column>
	  <el-table-column label="体测数值" align="center" prop="sj_dfsz" >
	  		<template #default="scope">
	  			<el-tag>{{ isDecimalZero(scope.row.sj_dfsz)?parseInt(scope.row.sj_dfsz):scope.row.sj_dfsz }} / {{scope.row.sj_unit}}</el-tag>
	    </template>
	  </el-table-column>
	  <!-- <el-table-column label="单位" align="center" prop="sjUnit" /> -->
      <el-table-column label="体测项目" align="center" prop="sj_tcxmid" width="160">
	    <template #default="scope">
			<el-tag>
				<span  v-for="c in projectList"
				  :key="c.id"
				  :label="c.xmMc"
				  :value="c.id"
				>{{scope.row.sj_tcxmid==c.id?c.xmMc:''}}</span>
			</el-tag>
	      <!-- <dict-tag :options="projectList" :value="scope.row.sjTcxmid"/> -->
	    </template>
	  </el-table-column>
	  
      <el-table-column label="学校" align="center" prop="sj_xxid" width="150" >
	  
	    <template #default="scope">
			
				<span  v-for="c in dept_self"
				  :key="c.value"
				  :label="c.label"
				  :value="c.value"
				>{{scope.row.sj_xxid==c.value?c.label:''}}</span>
	      <!-- <dict-tag :options="dept_self" :value="scope.row.sjXxid"/> -->
		  
		  
	    </template>
	  
	  </el-table-column>
      <el-table-column label="年级" align="center" prop="sj_njid" width="100" :show-overflow-tooltip="true">
        <template #default="scope">
          <dict-tag :options="tc_nj" :value="scope.row.sj_njid"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号" align="center" prop="sj_sfz" min-width="180" />
      <el-table-column label="体测名称" align="center" prop="tcnameid"  width="200">
	    <template #default="scope">
		  	{{scope.row.tcname}}
	    </template>
	  </el-table-column>
      <el-table-column label="评分标准" align="center" prop="sjTcfl"  width="200">
	    <template #default="scope">
			{{scope.row.xmfl_title}}
	    </template>
	  </el-table-column>
      <!-- <el-table-column label="基础数据" align="center" prop="sjJcsjid" /> -->
      <el-table-column label="体测时间" align="center" prop="sj_tcsj" width="180">
        <template #default="scope">
          <!-- <span>{{ parseTime(scope.row.sjTcsj, '{y}-{m}-{d}') }}</span> -->
          <span>{{scope.row.sj_tcsj }}</span>
        </template>
      </el-table-column>
	  <el-table-column label="设备" align="center" prop="sj_sbid" width="180" />
      <el-table-column label="体测教师" align="center" prop="sj_tcjs" width="100" />
      <!-- <el-table-column label="创建人" align="center" prop="sjCreateby" /> -->
<!--      <el-table-column label="创建时间" align="center" prop="sjCreate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.sjCreate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="修改人" align="center" prop="sj_updateby" /> -->
      <!-- <el-table-column label="修改时间" align="center" prop="sj_update" /> -->
      <!-- <el-table-column label="原始编号" align="center" prop="sjData" /> -->
      <!-- <el-table-column label="学号" align="center" prop="sjXh" /> -->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200" fixed="right">
        <template #default="scope">
          <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['tc:tcsj:edit']">优化</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tc:tcsj:remove']">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="handleQuery"
    />

    
  </div>
</template>

<!-- 测试数据(多次取最大数据值)  按体测名称、设备  导出 -->
<script setup name="Cssj">
import { checkPermi, checkRole } from "@/utils/permission"; // 1. 导入权限判断函数
import { listDept } from "@/api/system/dept";
import { listClass } from "@/api/tc/class";
import { listProject} from "@/api/tc/project";
import { listTcname} from "@/api/tc/tcname";
import { listShebei} from "@/api/tc/shebei";
import useUserStore from '@/store/modules/user';
import {selectTcData} from '@/api/tc/datafx';
var userStore=useUserStore();

const { proxy } = getCurrentInstance();
const { tc_nj,sys_user_sex } = proxy.useDict('tc_nj','sys_user_sex');

const tcsjList = ref([]);
const tcnameList = ref([]);
const projectList = ref([]);
const shebeiList = ref([]);
const classList = ref([]);



const open = ref(false);
const open2 = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const title2 = ref("");

// const projectList2 = ref([]);
// const studentList = ref([]);
const classList2 = ref([]);
// const classList3 = ref([]);
const dept_self = ref([]);
const isXxidFlag=ref(false);
const auform=ref(true);
const tmpGetPar={pageNum: 1,pageSize: 999,tcxxid:userStore.user.deptId};
const tmpProPar={pageNum: 1,pageSize: 999};
const tmpProTypePar={pageNum: 1,pageSize: 999,xmflStatus:'1'};

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    sjXxid: '',
    sjBjid: '',
    sjNjid: '',
    sjTcxmid: '',
    sjTcnameid: '',
    sjSbid: '',
    sjSfz: '',
    sjXsname: ''
  },
  rules: {},
  rules2: {},
  rules3: {},
});

const { queryParams, form, rules, rules2,rules3 } = toRefs(data);


 onMounted(() => {
      // console.log('组件已经挂载')
	getTcnameList();
 })
 
 



/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.sjXxid=dept_self.value[0].value;
  queryParams.value.sjTcnameid=tcnameList.value[0].id;
  schoolChange(classList.value[0].xxid)
  // handleQuery();
}

/** 查询体测项目数据列表 */
function getList() {
  loading.value = true;
	  // console.log(queryParams.value))
	  console.log("getList")
  selectTcData(queryParams.value).then(response => {
	  console.log(response.rows)
    tcsjList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

 /** 查询体测设备列表 */
 function getListShebei(deptid) {
   loading.value = true;
   listShebei({pageNum: 1,pageSize: 999,sbXxid:deptid}).then(response => {
    loading.value = false;
    shebeiList.value = response.rows;
 	// queryParams.value.sjSbid=shebeiList.value[0].id;
	handleQuery() ;
   });
 }
 /** 查询体测名称列表 */
 function getTcnameList() {
   loading.value = true;
   listTcname(tmpGetPar).then(response => {
    loading.value = false;
    tcnameList.value = response.rows;
 	queryParams.value.sjTcnameid=tcnameList.value[0].id;
	getProjectList()
   });
 }
 
 /** 查询体测项目列表 */
 function getProjectList() {
   loading.value = true;
   listProject(tmpProPar).then(response => {
     loading.value = false;
     projectList.value = response.rows;
	 getDeptInfo();  
   });
 }
 
 function getDeptInfo(){
 		// console.log(userStore.user)
 		if(checkRole(['admin','common'])){
			loading.value = true;
 			listDept(tmpGetPar).then(response => {
				loading.value = false;
 			  	response.data.forEach(function (element) {
 			  	  // 遍历数组，对每个元素进行操作
 				   if(element.xxlevel !=null && element.xxlevel != ''){
 						dept_self.value.push({"value":element.deptId,"label":element.deptName});
 				   }
 			  	});
 				queryParams.value.sjXxid=dept_self.value[0].value;
 				getClassList(null);
 			});
 			isXxidFlag.value=false;
 		}else{
 			dept_self.value.push({"value":userStore.user.deptId,"label":userStore.user.dept.deptName});
 			queryParams.value.sjXxid=dept_self.value[0].value;
 			isXxidFlag.value=true;
 			getClassList(queryParams.value.sjXxid);
 		}
 }
 
 function getClassList(bjid){
    loading.value = true;
 	// if(checkRole(['admin','common'])){
 	// 	bjid=null;;
 	// }else{
 	// 	bjid=userStore.user.deptId;
 	// }
 	listClass({"pageNum": 1,"pageSize": 500,"bjXxid" : bjid}).then(response => {
		loading.value = false;
 		response.rows.forEach(function (el) {
 		  // 遍历数组，对每个元素进行操作
 			classList.value.push({"value":el.id,"label":el.bjName,"xxid":el.bjXxid,"nj":el.bjNj});
 		});
 		if(classList.value.length > 0){
 			// queryParams.value.sjBjid=classList.value[0].value;
 			schoolChange(classList.value[0].xxid)
 		}
 	});
 }
 function schoolChange(e){
	getListShebei(e);
 	classList2.value= classList.value.filter(function(currentValue,index,arr){
 		// console.log(currentValue)
 		return currentValue.xxid == e ;
 	});
 	// classList2.value.unshift({"value":-1,"label":'全部',"xxid":e,"nj":"-1"});
 	// if(classList2.value.length==0){
 	// 	queryParams.value.sjBjid=null;
		// queryParams.value.sjNjid=null;
 	// }else{
 	// 	queryParams.value.sjBjid=classList2.value[0].value;
		// queryParams.value.sjNjid=classList2.value[0].nj;
 	// }
	// handleQuery();
 } 
 function classChange(e){
	 // console.log(classList2.value)
	//  let tmp= classList2.value.filter(function(currentValue,index,arr){
	//  	// console.log(currentValue)
	//  	return currentValue.value == e ;
	//  });
	 
	// queryParams.value.sjNjid=tmp[0].nj;
 }
 





/** 导出按钮操作 */
function handleExport() {
  proxy.download('tc/tcDataFX/selectTcData_export', {
    ...{data:JSON.stringify(queryParams.value)}
  }, `测试数据_${new Date().getTime()}.xlsx`)
}





function isDecimalZero(num) {
    // 将数字转换为字符串
    const numStr = num.toString();
    // 提取小数点后的部分
    const decimalPart = numStr.split('.')[1];
    // 检查小数点后是否全是0
    return /^0*$/.test(decimalPart);
}
</script>
